<%--
  Created by IntelliJ IDEA.
  User: 20426
  Date: 2020/7/25
  Time: 16:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>场次管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/back/layuiadmin/layui/css/layui.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">影厅名称</label>
                    <div class="layui-input-inline">
                        <select name="auditoriumsId" id="select_search_auditoriumsId">
                            <option value="">全部</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">电影名称</label>
                    <div class="layui-input-inline">
                        <select name="filmId" id="select_search_filmId">
                            <option value="">全部</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">开始时间</label>
                    <div class="layui-input-inline">
                        <input type="text" name="startTime" id="startTime" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">结束时间</label>
                    <div class="layui-input-inline">
                        <input type="text" name="endTime" id="endTime" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>

            <div class="layui-card-body">
                <script id="screening_tool_title" type="text/html">
                    <div class="layui-btn-container">
                        <button type="button" id="add" class="layui-btn"><i class="layui-icon"></i>添加场次</button>
                    </div>
                </script>
                <table id="screening_list" lay-filter="screening_list"></table>
                <script id="screening_tool" type="text/html">
                    {{#  if(new Date(d.screeningStartTime).getTime()>new Date().getTime()){  }}
                    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="update"><i class="layui-icon layui-icon-edit"></i>修改</a>
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
                    {{#  } }}
                </script>
            </div>
        </div>
    </div>
</div>
<script src="${pageContext.request.contextPath}/back/layuiadmin/layui/layui.js"></script>
<script type="text/javascript">
    layui.config({
        base: '${pageContext.request.contextPath}/back/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['table','form','laydate'], function(){
        var table = layui.table,form = layui.form,$ = layui.$,laydate = layui.laydate;
        var cols = [[ //表头
            {field: 'screeningStartTime', title: '开始时间', sort: true, width:200, fixed: 'left'}
            ,{field: 'auditoriumsName', title: '影厅名称', sort: true, width:200,templet:"<div>{{d.auditoriums.auditoriumsName}}</div>"}
            ,{field: 'filmName', title: '电影名称', sort: true, width:200,templet:"<div>{{d.film.filmName}}</div>"}
            ,{field: 'screeningPrice', title: '价格', sort: true, width:200}
            ,{field: '', title: '操作', sort: true, width:200,toolbar:"#screening_tool"}
        ]];


        table.render({
            elem: '#screening_list'
            ,url: '${pageContext.request.contextPath}/back/screening' //数据接口
            ,toolbar: "#screening_tool_title"
            ,page: true //开启分页
            ,cols: cols
            ,parseData:function(res){
                return {
                    "code": 0, //解析接口状态
                    "msg": "搜索成功", //解析提示文本
                    "count": res.total, //解析数据长度
                    "data": res.list, //解析数据列表
                };
            }
        });

        // 获取全部电影
        var filmList;
        $.ajax({
            url:"${pageContext.request.contextPath}/back/filmList",
            data:{filmStatus:"0",page:1,limit:1000},
            success:function(result){
                filmList = result.list;
                $.each(filmList,function(index,val){
                    $("#select_add_filmId").append("<option value='"+val.filmId+"'>"+val.filmName+"</option>");
                    $("#select_update_filmId").append("<option value='"+val.filmId+"'>"+val.filmName+"</option>");
                    $("#select_search_filmId").append("<option value='"+val.filmId+"'>"+val.filmName+"</option>");
                });
                layui.form.render("select");
            },
            error:function(){

            }
        });
        // 获取全部影厅
        var auditoriumsList;
        $.ajax({
            url:"${pageContext.request.contextPath}/back/auditoriums",
            success:function(result){
                auditoriumsList = result.list;
                $.each(auditoriumsList,function(index,val){
                    $("#select_add_auditoriumsId").append("<option value='"+val.auditoriumsId+"'>"+val.auditoriumsName+"</option>");
                    $("#select_update_auditoriumsId").append("<option value='"+val.auditoriumsId+"'>"+val.auditoriumsName+"</option>");
                    $("#select_search_auditoriumsId").append("<option value='"+val.auditoriumsId+"'>"+val.auditoriumsName+"</option>");
                });
                layui.form.render("select");
            }

        });

        laydate.render({
            elem: '#add_screeningStartTime' //指定元素
            ,type:'datetime'
        });
        laydate.render({
            elem: '#update_screeningStartTime' //指定元素
            ,type:'datetime'
        });
        laydate.render({
            elem: '#startTime' //指定元素
            ,type:'datetime'
        });
        laydate.render({
            elem: '#endTime' //指定元素
            ,type:'datetime'
        });

        // 添加
        $("#add").click(function(){
            layer.open({
                type: 1
                ,title:"添加场次"
                ,content:$("#add_screening_form")
                ,area: ['500px', '420px']
                ,btn: ['确定', '取消']
            });
        });
        form.on("submit(add_screening)",function(data){
            let field = data.field;
            field["film"]={filmId:field.filmId};
            field["auditoriums"]={auditoriumsId:field.auditoriumsId};
            $.ajax({
                type:"post"
                ,url:"${pageContext.request.contextPath}/back/screening"
                ,contentType:"application/json;charset=utf-8"
                ,data:JSON.stringify(field)
                ,success:function(result){
                    if(result){
                        layer.confirm("添加成功",function(){
                            location.reload();
                        },function () {
                            location.reload();
                        });
                    }else{
                        layer.msg("添加失败");
                    }
                }
                ,error:function(){
                    layer.msg("ajax错误");
                }
            });
        });

        // 修改
        table.on("tool(screening_list)",function(obj){
            var data = obj.data;
            if(obj.event == "update"){
                layer.open({
                    type: 1
                    ,title:"修改场次"
                    ,content:$("#update_screening_form")
                    ,area: ['500px', '420px']
                    ,btn: ['确定', '取消']
                });
                form.val("update_screening_form",{
                    screeningId:data.screeningId,
                    screeningStartTime:data.screeningStartTime,
                    filmId:data.film.filmId,
                    auditoriumsId:data.auditoriums.auditoriumsId,
                    screeningPrice:data.screeningPrice
                });
            }
            if(obj.event == "delete"){
                layer.confirm("是否确认删除？",function(){
                    $.ajax({
                        type:"delete"
                        ,url:"${pageContext.request.contextPath}/back/screening/"+data.screeningId
                        ,success:function(result){
                            if(result){
                                layer.confirm("删除成功",function(){
                                    parent.location.reload();
                                },function () {
                                    parent.location.reload();
                                });
                            }else{
                                layer.msg("删除失败");
                            }
                        }
                        ,error:function(){
                            layer.msg("ajax错误");
                        }
                    })
                })
            }
        });
        form.on("submit(update_screening)",function(data){
            let field = data.field;
            field["film"]={filmId:field.filmId};
            field["auditoriums"]={auditoriumsId:field.auditoriumsId};
            $.ajax({
                type:"put"
                ,url:"${pageContext.request.contextPath}/back/screening"
                ,contentType:"application/json;charset=utf-8"
                ,data:JSON.stringify(field)
                ,success:function(result){
                    if(result){
                        layer.confirm("修改成功",function(){
                            parent.location.reload();
                        },function () {
                            parent.location.reload();
                        });
                    }else{
                        layer.msg("修改失败");
                    }
                }
                ,error:function(){
                    layer.msg("ajax错误");
                }
            });
        });

        // 查询
        form.on("submit(search)",function(data){
            var field = data.field;
            table.render({
                elem: '#screening_list'
                ,url: '${pageContext.request.contextPath}/back/screening' //数据接口
                ,where:field
                ,toolbar: "#screening_tool_title"
                ,page: true //开启分页
                ,cols: cols
                ,parseData:function(res){
                    return {
                        "code": 0, //解析接口状态
                        "msg": "搜索成功", //解析提示文本
                        "count": res.total, //解析数据长度
                        "data": res.list, //解析数据列表
                    };
                }
            });
        });


    });
</script>
<!-- 添加场次from表单 -->
<div class="layui-form" id="add_screening_form" style="display:none"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <div class="layui-form-item">
        <label class="layui-form-label">场次时间</label>
        <div class="layui-input-block">
            <input type="text" required name="screeningStartTime" id="add_screeningStartTime" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">影厅</label>
        <div class="layui-input-block">
            <select name="auditoriumsId" required id="select_add_auditoriumsId">
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">电影</label>
        <div class="layui-input-block">
            <select name="filmId" required id="select_add_filmId">
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">价格</label>
        <div class="layui-input-block">
            <input type="text" required name="screeningPrice" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn"  lay-submit lay-filter="add_screening">确认添加</button>
        </div>
    </div>
</div>

<!-- 修改场次from表单 -->
<div class="layui-form" id="update_screening_form" lay-filter="update_screening_form"  style="display:none"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <input type="hidden" name="screeningId">
    <div class="layui-form-item">
        <label class="layui-form-label">场次时间</label>
        <div class="layui-input-block">
            <input type="text" required name="screeningStartTime" id="update_screeningStartTime" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">影厅</label>
        <div class="layui-input-block">
            <select name="auditoriumsId" required id="select_update_auditoriumsId">
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">电影</label>
        <div class="layui-input-block">
            <select name="filmId" required id="select_update_filmId">
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">价格</label>
        <div class="layui-input-block">
            <input type="text" required name="screeningPrice" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn"  lay-submit lay-filter="update_screening">确认修改</button>
        </div>
    </div>
</div>

</body>
